<?php
// incluindo as bibliotecas comuns
require_once 'util/inicio.php';
//require_once 'Categoria.php';
require_once 'dados.php';

// controlando qual tela exibir
$tela = "";
switch(@$_GET['acao']) {
  case "listarProdutos":
  	$tela = "listaDeProdutos";
  	break;
  case "detalharProduto":
	$tela = "detalhesDeProduto";
  	break;  
  default:
    $tela = 'home';
}
?>
<html>
<head>
<title>:. Loja Virtual .:</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/estilo.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="109" background="imagens/topo_bg.gif"><table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
      <tr>      	
        <td width="12"></td>        
        <td><img src="imagens/logo.gif" width="143" height="42"></td>
        <!-- COMEÇA O LOGIN --->
        <td align="right" valign="top">
        	<table  border="0" cellspacing="0" cellpadding="0">       
         	<tr>          
            	<td><a href="?acao=cadastro"><img src="imagens/cadastrese.gif" width="92" height="22" border="0"></a></td>
            	<td><img src="imagens/login.gif" alt="Login" width="160" height="22" border="0"></td>
          	</tr>
        	</table>
            <table border="0" cellspacing="0" cellpadding="0">
				<form name="formlogin" id="formlogin" action="?acao=logar">
	                <tr>
	                	<td width="153" height="57" align="center" background="imagens/login_bg.gif">
		                	<table width="129" height="39" border="0" cellpadding="0" cellspacing="0">
		                        <tr>
		                            <td><font color="white" size="2">Usuário:</font></td>
		                            <td>
		                            	 <input type="text" name="login" id="login" class="classlogin">
		                            </td>
		                        </tr>
		                        <tr>
		                            <td><font color="white" size="2">Senha:</font></td>
		                            <td><input type="password" name="login" id="senha" class="classlogin">
		                            <input name="imageField" type="image" src="imagens/ok_cinza.gif" alt="OK"></td>
		            			</tr>
		            		</table>
	            		</td>
	                </tr>
				</form>
            </table></td>
      </tr>
    </table> </td>
  </tr>
  <tr>
    <td height="25" background="imagens/barra_cores.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top"><table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td valign="top" bgcolor="#EAEAEA">
         <table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
		  <tr>
			<td valign="top" bgcolor="#EAEAEA"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td height="30" bgcolor="#7D7D7D"><img src="imagens/spacer.gif" width="10" height="1"><img src="imagens/categorias.gif" width="68" height="14"></td>
			  </tr>
			  <tr>
				<td height="1" bgcolor="#FFFFFF"><img src="imagens/spacer.gif" width="153" height="1"></td>
			  </tr>
			 <?	 
		     $categorias = Categoria::listarCategorias();				  	
			 foreach($categorias as $categoria) {  		
			?>
			  <tr>
				<td height="20"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
				  <tr>
					<td width="10"><img src="imagens/spacer.gif" width="10" height="1"></td>
					<td>
						<a href="?acao=listarProdutos&categoria=<?=$categoria->getId()?>" class="texto">
						 <?=$categoria->getNome()?></a>
					</td>
					<td width="10"><img src="imagens/spacer.gif" width="10" height="1"></td>
				  </tr>
				</table></td>
			  </tr>
			  <tr>
				<td height="1" bgcolor="#FFFFFF"><img src="imagens/spacer.gif" width="1" height="1"></td>
			  </tr>
			  <?
					}
			  ?>

			</table></td>
		   </tr>
		 </table>
        </td>
        <td width="1" valign="top" bgcolor="#DCDCDDC"><table width="1"  border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td height="30" bgcolor="#FFFFFF"><img src="imagens/spacer.gif" width="1" height="1"></td>
          </tr>
        </table></td>
        <td valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="30" bgcolor="#AEAEAE"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
              <form name="busca" action="">
			  <input type="hidden" name="acao" value="buscarProdutos">
			  <tr>
                <td width="10"></td>
                <td width="35"><img src="imagens/lupa.gif" width="30" height="23"></td>
                <td width="53"><img src="imagens/buscar.gif" width="45" height="13"></td>
                <td width="145"><input name="termo" type="text" class="textfield" id="termo" value="<?=@$_GET['termo']?>"></td>
                <td width="145">
                <select name="categoria" class="textfield">
					<option value="">:: Todas ::</option>
			<? foreach($categorias as $categoria) { ?>
				<option value="<?=$categoria->getId()?>">:: <?=$categoria->getNome()?> ::</option>
			  <? }  ?>
			    </select></td>
                <td><input name="ok" type="image" id="ok" src="imagens/ok_cinza.gif" width="29" height="18" border="0"></td>
                </tr>
			  </form>
            </table></td>
          </tr>
          <tr>
            <td height="1" bgcolor="#FFFFFF"><img src="imagens/spacer.gif" width="1" height="1"></td>
          </tr>
        </table>
		<?php
			include "telas/$tela.php";
		?>
		</td>
        <td width="1" valign="top" bgcolor="#DCDCDDC"><table width="1"  border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td height="30" bgcolor="#FFFFFF"><img src="imagens/spacer.gif" width="1" height="1"></td>
          </tr>
        </table></td>
        <td width="195" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="30" bgcolor="#AEAEAE"><a href="?acao=exibirCarrinho"><img src="imagens/carrinho.gif" alt="Meu carrinho" width="122" height="19" border="0"></a></td>
          </tr>
          <tr>
            <td height="1" bgcolor="#FFFFFF"></td>
          </tr>
          <tr>
              <td>
              <!-- Come�a o carrinho -->
              	<table width="100%" border="0" cellpadding="0" cellspacing="0">
              	<?
                 	$carrinho = Carrinho::getCarrinhoAtual();
                 	$item1 = new Item($dados['produtos'][0],2,300);
                 	$item2 = new Item($dados['produtos'][3],5,10.00);
                 	$carrinho->setItens($item1);
                 	$carrinho->setItens($item2);
                 	
                 	$itens = 0;
                 	$total_itens = 0;
                 	if(count($carrinho->getItens())>0) {
                 		$total = $carrinho->calcularTotal();
                 		$total_itens = count($itens = $carrinho->getItens());
                 		$bg = "#F2F2F2";                 	
				?>
                  <tr bgcolor="#D1E5F3" class="textocarrinho">
                      <td bgcolor="#D1E5F3" class="produto">Produto</td>
                      <td width="26" align="center">Qtd</td>
                      <td width="51" align="right" class="valor">Valor</td>
                  </tr>
               <?
	           	foreach($itens as $item) {
					$bg = ($bg = "#F2F2F2")?"#FFFFFF":"#F2F2F2";
					$nome = substr($item->getProduto()->getNome(),0 , 18);
					if (strlen($item->getProduto()->getNome()) > 18) {
						$nome .= "...";
					}
               ?>
                  <tr class="textocarrinho" bgcolor="<?=$bg?>">
                      <td class="produto">
                      	<?=$nome?>
                      </td>
                      <td align="center" class="qtd">
                      	<?=sprintf('%02d', $item->getQuantidade())?>
                      </td>
                      <td align="right" class="valor">
                      	<?=sprintf('%.2f', $item->calcularPreco())?>
					  </td>
                  </tr>
               <?
	           	}
               ?>               
              </table></td>
          </tr>
          <tr>
            <td height="1" bgcolor="#FFFFFF"></td>
          </tr>
          <?
			}
          ?>
          <tr class="textocarrinho">
              <td align="right" bgcolor="#D1E5F3">
              	Itens:
              	<span class="valorfinal">
              		<?=printf('%02d',$total_itens)?>
              	</span>
				Total
				<span class="valorfinal">
					<?=printf('%02d',$total)?>
				</span>
			  </td>
          </tr>
          <tr>
              <td>&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td valign="top" height="1" bgcolor="#FFFFFF"></td>
  </tr>
  <tr>
    <td height="20" bgcolor="#7D7D7D"><table width="100%"  border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="10"></td>
        <td class="preco">Designer: Blink</td>
        <td width="90"><a href="http://www.php.net" target="_blank"><img src="imagens/phppowered.png" alt="PHP5 Powered" width="80" height="15" border="0"></a></td>
        <td width="90"><a href="http://pear.php.net" target="_blank"><img src="imagens/pearpowered.png" alt="PEAR" width="80" height="15" border="0"></a></td>
        <td width="90"><a href="http://planet-php.net" target="_blank"><img src="imagens/planet-php-button-2.jpg" alt="Planet PHP" width="80" height="15" border="0"></a></td>
        </tr>
    </table></td>
  </tr>
</table>
</body>
</html>